package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> getAll() {
        List<Role> raw =   roleMapper.selectAll();
        return raw;
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public Role getById(Integer id) {
        Role role =roleMapper.selectById(id);
        return role;
    }

    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.addRole(role);
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public  PageResult<Role> page(String name, String label, Integer page, Integer pageSize){
        Integer count = roleMapper.count(name,label);

        Integer offset = (page-1)*pageSize;
        List<Role> row = roleMapper.selectByPage(name,label,offset,pageSize);

        return PageResult.<Role>builder()
                .total(count)
                .rows(row)
                .build();

    }


}
